package main;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.lang.reflect.Proxy;
import java.net.InetSocketAddress;
import java.net.Socket;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.UnsupportedLookAndFeelException;

public class Client
{
	private String id = null;
	private String host = null;
	private String pw = null;
	private int port = 717;

	static public Socket serverSocket;
	private QueryFrame queryFrame = null;
	private ObjectInputStream ois;
	private ObjectOutputStream oos;
	public Client(String server, String port, String id, String pw) throws IllegalArgumentException, IOException
	{
		
		host = server;
		this.port = Integer.parseInt(port);
		this.id=id;
		this.pw=pw;
		initialize();
	}
	public void writeObject(Object object)
	{
		try 
		{
			oos.flush();
			oos.writeObject(object);
		} 
		catch (IOException e) 
		{
			JOptionPane.showMessageDialog(null, "Connection to server lost.");
		}
	}
	
	public Object readObject()
	{
		try 
		{
			return ois.readObject();
		} 
		catch (ClassNotFoundException e) 
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			JOptionPane.showMessageDialog(null, "Connection to server lost.");
		}
		return null;
	}
	public void  initialize() throws IllegalArgumentException, IOException
	{
		serverSocket=null;
		try 
		{
			serverSocket= new Socket(host, port);
		} 
		catch (IOException e) 
		{
			JOptionPane.showMessageDialog(null,	"Can not connect to the server. Please check the IP and Port of the Server again.");
			return;
		}
		InputStream is=serverSocket.getInputStream();
		ois = new ObjectInputStream(is);
		OutputStream os=serverSocket.getOutputStream();
		oos = new ObjectOutputStream(os);
		
		String sentMessage=id+" "+pw;
		String receivedMessage = " ";
 		System.out.println("Talking to Server");
 		do
		{
			
			oos.writeObject(sentMessage);
			
			if (receivedMessage.equalsIgnoreCase("user"))
			{
				JOptionPane.showMessageDialog(null,	"ID not existed!");
				return;
			}
			else if (receivedMessage.equalsIgnoreCase("password"))
			{
				JOptionPane.showMessageDialog(null,	"Wrong Password!");
				return;
			}
			else if (receivedMessage.equalsIgnoreCase("done"))
			{
				JOptionPane.showMessageDialog(null,	"Connected!");
				break;
			}
			else
			{
				try 
				{
					receivedMessage= (String)ois.readObject();
				} 
				catch (ClassNotFoundException e) 
				{
					
				}
				System.out.println("Received : " + receivedMessage);					
			}
		}
		while(true);
		    
		try 
		{
			queryFrame = new QueryFrame(serverSocket.getInetAddress(),serverSocket,this);
			
		} 
		catch (ClassNotFoundException e) 
		{
			e.printStackTrace();
		} 
		catch (InstantiationException e) 
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e) 
		{
			e.printStackTrace();
		} 
		catch (UnsupportedLookAndFeelException e) 
		{
			e.printStackTrace();
		}
	}
	public void dispose() 
	{
		queryFrame.dispose();
		
	}
}
